package com.example.util;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.UnsupportedEncodingException;
import java.security.Security;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * Created by youkun on 2018/3/17.
 * 发送邮件工具类
 * 163 smtp.163.com  25
 * qq smtp.qq.com 587
 */
public class EmailUtil {


    /**
     *
     * @param hostName 服务器地址
     * @param port  端口
     * @param fromEmail 发送人
     * @param toEmail   接受人
     * @param subject   主题
     * @param msg   信息
     * @param passWord  发送人密码
     * @throws Exception
     */
    public static void sendEmail(String hostName,int port, String fromEmail,String toEmail,String subject ,String msg,String passWord) throws Exception{
//        Email email = new SimpleEmail();
//        email.setHostName(hostName);
//        email.setSmtpPort(port);
//        email.setAuthenticator(new DefaultAuthenticator(fromEmail, passWord));
//        email.setSSLOnConnect(true);
//
//        try {
//            email.setFrom(fromEmail);
//            email.setSubject(subject);
//            email.setMsg(msg);
//            email.addTo(toEmail);
//            email.send();
//        } catch (Exception e) {
//           throw new Exception(e.getMessage());
//        }
        try {
            Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

            Properties props = System.getProperties();
            props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
            props.setProperty("mail.smtp.socketFactory.fallback", "false");
            props.setProperty("mail.smtp.port", port+"");
            props.setProperty("mail.smtp.host", hostName);
            props.setProperty("mail.smtp.socketFactory.port", port+"");
            props.put("mail.smtp.auth", "true");
            props.put("mail.transport.protocol", "smtp");

            Session session = Session.getDefaultInstance(props, new Authenticator(){
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(fromEmail, passWord);
                }});

            InternetAddress fromAddress = new InternetAddress(fromEmail);
            InternetAddress toAddress = new InternetAddress(toEmail);
            MimeMessage message = new MimeMessage(session);
            message.setFrom(fromAddress);
            message.addRecipient(Message.RecipientType.TO, toAddress);
            message.setSentDate(new Date());
            message.setSubject(subject);
            message.setText(msg);

            // 第三步：发送消息
            Transport transport = session.getTransport("smtp");
            transport.send(message);

        }
        catch(Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }

    }

    public static void main(String arg[]){

        try {
            Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

            // Get a Properties object
            Properties props = System.getProperties();
            props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
            props.setProperty("mail.smtp.socketFactory.fallback", "false");
            props.setProperty("mail.smtp.port", "465");
            props.setProperty("mail.smtp.host", "smtp.163.com");
            props.setProperty("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.auth", "true");
            props.put("mail.transport.protocol", "smtp");

            Session session = Session.getDefaultInstance(props, new Authenticator(){
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("Hytajc@163.com", "hytajc123");
            }});

            InternetAddress fromAddress = new InternetAddress("Hytajc@163.com");
            InternetAddress toAddress = new InternetAddress("13816524526@163.com");


            MimeMessage message = new MimeMessage(session);
            message.setFrom(fromAddress);
            message.addRecipient(Message.RecipientType.TO, toAddress);
            message.setSentDate(new Date());
            message.setSubject("hello");
            message.setText("nihao");

            // 第三步：发送消息
            Transport transport = session.getTransport("smtp");
            transport.send(message);
            System.out.println("message yes");

        }
        catch(Exception e){
                e.printStackTrace();
        }

    }



}
